import { DeleteDto, IndexDto, ShowDto } from '@libs/common/dto';
import { ApiProperty, PartialType } from '@nestjs/swagger';
import {
  IsBoolean,
  IsEnum,
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
} from 'class-validator';

import { PointType, PointTypeEnum } from '@/constant.enum';

import { PointsHistory } from '../entities/points-history.entity';

export class IndexPointsHistoryDto extends IndexDto<PointsHistory> {}

export class ShowPointsHistoryDto extends ShowDto {}

export class DeletePointsHistoryDto extends DeleteDto {}

class createPointsHistoryDto {
  @IsNotEmpty({ message: '用户ID不能为空' })
  userId: string;

  @IsNotEmpty({ message: '用户名称不能为空' })
  userName: string;

  @IsNotEmpty({ message: '积分不能为空' })
  @IsNumber({}, { message: '积分必须是数字' })
  points: number;
}

export class ManualPointsHistoryDto extends createPointsHistoryDto {
  @IsNotEmpty({ message: '操作人ID不能为空' })
  operatorId: string;

  @IsNotEmpty({ message: '操作人名称不能为空' })
  operatorName: string;

  @IsString({ message: '描述必须是字符串' })
  @IsNotEmpty({ message: '描述不能为空' })
  desc: string;

  type: 'manual' | 'system' = PointTypeEnum['手动'];
}

export class ConsumePointsHistoryDto extends createPointsHistoryDto {
  @IsEnum(PointType, { message: '积分类型不正确' })
  @IsNotEmpty({ message: '积分类型不能为空' })
  type: 'consume';

  @IsNotEmpty({ message: '订单号不能为空' })
  @IsString({ message: '订单号必须是字符串' })
  orderNo: string;

  @ApiProperty({ description: '商品名称' })
  productName: string;

  @ApiProperty({ description: '商品数量' })
  productNumber: number;

  @IsString({ message: '描述必须是字符串' })
  @IsOptional()
  desc?: string;
}

export class RefundPointsHistoryDto extends createPointsHistoryDto {
  @IsEnum(PointType, { message: '积分类型不正确' })
  @IsNotEmpty({ message: '积分类型不能为空' })
  type: 'refund';

  @IsNotEmpty({ message: '订单号不能为空' })
  @IsString({ message: '订单号必须是字符串' })
  orderNo: string;

  @ApiProperty({ description: '商品名称' })
  productName: string;

  @ApiProperty({ description: '商品数量' })
  productNumber: number;

  @IsNotEmpty({ message: '操作人ID不能为空' })
  operatorId: string;

  @IsNotEmpty({ message: '操作人名称不能为空' })
  operatorName: string;

  @IsString({ message: '描述必须是字符串' })
  @IsOptional()
  desc: string;
}

export class AttendanceCreatePointsHistoryDto extends createPointsHistoryDto {
  @IsNotEmpty({ message: '积分类型不能为空' })
  type: (typeof PointType)[number];

  @IsNotEmpty({ message: '钉钉用户ID不能空' })
  dingId: string;

  @IsString({ message: '描述必须是字符串' })
  @IsOptional()
  desc: string;

  @IsNotEmpty({ message: '操作人ID不能为空' })
  operatorId: string;

  @IsNotEmpty({ message: '操作人名称不能为空' })
  operatorName: string;
}

export class UpdatePointsHistoryDto extends PartialType(
  ManualPointsHistoryDto,
) {
  @ApiProperty()
  @IsNotEmpty({ message: 'id 不能为空' })
  id: string;
}

export class SwitchPointsHistoryDto {
  @ApiProperty()
  @IsNotEmpty({ message: 'id 不能为空' })
  id: string;

  @ApiProperty({ description: '状态' })
  @IsNotEmpty({ message: '状态不能为空' })
  @IsBoolean()
  status: boolean;
}

export class ImportPointsHistoryDto {}

export class ExportPointsHistoryDto {}
export class FindUserPointsHistoryDto {
  userId: string[];
}

export class FindPointsHistoryDto {
  userId?: string;
}
